/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apt.countrytag;

import com.apt.bean.Countries;
import java.util.HashMap;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Bach
 */
public class CountryTagHandler extends SimpleTagSupport {
    private String page;
    private String countryname;
    private String country_id;

    /**
     * Called by the container to invoke this tag. The implementation of this
     * method is provided by the tag library developer, and handles all tag
     * processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();
        int cursor1;
        HashMap<Integer, Countries > hm = GetCountry.getListCountries();
        try {
            if (hm != null && hm.size() != 0) {
                out.println(Process_PageCountry.divPage(page, hm.size(), 12, countryname, country_id));
                int uid_f = 1;
                int uid_l = 30;

                try {
                    cursor1 = Integer.parseInt(page);

                } catch (NumberFormatException ex) {
                    cursor1 = 1;
                }
                if (cursor1 > 1) {
                    uid_f = (cursor1 - 1) * 30;
                    uid_l = cursor1 * 30;
                }
                int current_page = uid_l / 30;
                for (int i = uid_f; i < uid_l; i++) {
                    Countries u = hm.get(i);
                    if (u != null) {
                        int status = Integer.parseInt(u.getCountry_Status());
                        String country_Name = u.getCountry_Name();
                        String country_ID = u.getCountry_Id();
                        StringBuilder sb = new StringBuilder(" <tr>");
                        
                        if (status == 1) {
                            sb.append("<form action=\"EditCountryNameServlet\" method=\"get\">\n"
                                    + "<td> \n"
                                    + "<input type=\"text\"  name=\"country_Name\" value=\"" + country_Name + "\"/>\n"
                                    + "<input type=\"submit\" class=\"button\" value=\"Update Name\"/>\n"
                                    + "<input type=\"hidden\" name=\"country_ID\" value=\""+country_ID+"\"   />"
                                    + "<input type=\"hidden\" name=\"pageno\" value=\""+current_page+"\"   />"
                                    + " </td>\n"
                                    + "</form>");
                            sb.append("<td><a href=\"#\" title=\"title\">Enable</a></td>");
                        } else {
                            sb.append("<td>" + u.getCountry_Name()+ "</td>");
                            sb.append("  <td><a href=\"#\" title=\"title\">Disable</a></td>");
                        }
                        sb.append("    <td>");
                        sb.append("        <!-- Icons -->");
                        if (status == 1) {
//                            u.getLiking_Id();
                            sb.append("<a href=\"DisableCountryServlet?pageno=" + current_page + "&countryname=" + country_Name + "&country_id=" + country_ID + "&likingId=" +country_ID + "\" title=\"Disable\"><img src=\"resources/images/icons/cross.png\" alt=\"Disable\" /></a>");
                            sb.append("---");
                            sb.append("  <a href=\"countries_city_edit.jsp?"
                                    + "country_ID=" + u.getCountry_Id() + "&country_Name=" + country_Name + ""
                                    + "&pageno=" + current_page + "\" \" title=\"Edit\">"
                                    + "<img src=\"resources/images/icons/edit.jpg\" alt=\"Enable\" /></a>");
                        } else {
                            sb.append("<a href=\"EnableCityServlet?pageno=" + current_page + "&countryname=" + country_Name + "&country_id=" + country_ID + "&likingId=" + country_ID+ "\" title=\"Enable\"><img src=\"resources/images/icons/pencil.png\" alt=\"Enable\" /></a>");
                        }
//                        sb.append(" <a href=\"Navigation?menu=17&pageno=" + current_page +"&payname="+u.getPayment_Type_Name()+ "\" title=\"Enable\" ><img src=\"resources/images/icons/view.png\" alt=\"Enable\" /></a>");
                        sb.append("   </td>");
                        sb.append(" </tr>");
                        out.print(sb.toString());

                    }
                }
            }
            JspFragment f = getJspBody();
            if (f != null) {
                f.invoke(out);
            }

        } catch (java.io.IOException ex) {
            throw new JspException("Error in ProfessionnalTagHandler tag", ex);
        }
    }

    public void setPage(String page) {
        this.page = page;
    }

    public void setCountryname(String countryname) {
        this.countryname = countryname;
    }

    public void setCountry_id(String country_id) {
        this.country_id = country_id;
    }
}
